package com.shisfish.library.Prere;

import java.util.Date;
import java.util.List;

import com.shisfish.library.Impl.UserImpl;
import com.shisfish.library.Model.BookLend;
import com.shisfish.library.Model.User;
import com.shisfish.library.Model.UserSingleLend;

public class BeforeLoan {

	// false 是可借
	public static User isOverdueOrQuota(String employee) {
		
		boolean isOverdueOrQuota = false;
		
		User lastUser = new User();
		
		List<User> users = new UserImpl().selectByKey(employee);
		
		if (users.size() > 0) {
			User user = users.get(0);
			
			List<BookLend> bookLends = user.getBookLends();
			
			if (bookLends.size() > 0) {
				
				Date date = new Date();
				
				for (BookLend bookLend : bookLends) {
					
					if (date.after(bookLend.getShouldReturnTime())) {
						
						// 有超期的书
						isOverdueOrQuota = true;
						break;
					}
					
				}
				
			}
			
			if (!isOverdueOrQuota) {
				// 书有木有预约满
				if (user.getUserSingleLend().getLoan_number() > 0) {
					UserSingleLend userSingleLend = user.getUserSingleLend();
					int loan = userSingleLend.getLoan_number();
					int loaned = userSingleLend.getLoaned_number();
					
					if (loaned >= loan) {
						// 不可预约
						isOverdueOrQuota = true;
					}
				}
				else {
					
					// 没有预约历史， 可预约
					isOverdueOrQuota = false;
					
				}
				
			}
			
			// 如果可借，把user传过去，否则，user为空
			if (!isOverdueOrQuota) {
				lastUser = user;
			}
			
		}
		
		return lastUser;
	}
	
}
